#
# Cookbook Name:: db2
# Recipe:: install
#
# Copyright 2013, IBM Corporation
#
# All rights reserved - Do Not Redistribute
#

# Most of this recipe is specific to SCE instances
include_recipe "gitclientsetup"
deployment = node[:deployment]
cuser = node['chef']['user']
cgroup = node['chef']['group']
zkep = "#{node[:zookeeper][:ip]}:#{node[:zookeeper][:port]}"

inst_temp = node['common']['inst_temp']+"/db2"
tmp = "/tmp"

ssh_repo = "#{node['common']['scpuser']}@#{node['common']['scphost']}"
scp_repo_dir = node['common']['scp_repo_dir']
  
startinstr :db2

package "libaio" do
  action :install
end

directory "#{inst_temp}" do
  user cuser
  action :create
end

#creating directory that will hold the migration shell script
directory "/migration" do
  user cuser
  group cgroup
  action :create
end

# Update the silent install template
template "#{tmp}/db2_10_5.rsp" do
  source "db2_10_5.rsp.erb"
  owner cuser
  group cgroup
  variables(
    :db2InstallDir => node[:db2][:installDir], 
    :db2Pass => node[:db2][:password], 
    :db2FencPass => node[:db2][:fencPassword], 
    :dasPass => node[:db2][:dasPassword])
  action :create
end

#copying the migration and start/stop shell scripts to /migration folder
cookbook_file "/migration/database.sh" do
  backup false
  owner cuser
  group cgroup
  mode 0755
  source "database.sh"
  action :create
end
cookbook_file "/migration/env.sh" do
  backup false
  owner cuser
  group cgroup
  mode 0755
  source "env.sh"
  action :create
end
cookbook_file "/migration/startDb2.sh" do
  backup false
  owner cuser
  group cgroup
  mode 0755
  source "startDb2.sh"
  action :create
end
cookbook_file "/migration/stopDb2.sh" do
  backup false
  owner cuser
  group cgroup
  mode 0755
  source "stopDb2.sh"
  action :create
end

id = 0
[ node[:db2][:installer_path], 
  node[:db2][:fixpack_path]].each do |file|
  id = id+1
  bash "fetch and decompress #{file}" do
    user cuser
    cwd inst_temp
    creates "#{tmp}/fetch-#{id}.done"
    code <<-EOH
      set -e
      # scp #{node[:common][:scp_repo]}/#{file} . && touch /tmp/fetch-#{id}
      ssh #{ssh_repo} "cat #{scp_repo_dir}/#{file}" | tar xvzf -
      [ $? == 0 ] && touch #{tmp}/fetch-#{id}.done
    EOH
  end
end

# Install the db2 server - Set to autostart after reboot (where is the initscript installed ???)
bash "install db2 server" do
  user "root"
  cwd "#{inst_temp}/server"
  code <<-EOH
    ./db2setup -r #{tmp}/db2_10_5.rsp
  EOH
  not_if "test -d #{node[:db2][:installDir]}"
  #TODO: Tivoli Systems Automation install is not complete - look into this later 
  returns 0
end

# Install fix pack
bash "install db2 fixpack" do
  user "root"
  cwd "#{inst_temp}/universal"
  creates "#{tmp}/fixpack_inst_marker"
  code <<-EOH
    echo 
    echo "Ensuring that DB2 is stopped..."
    /migration/stopDb2.sh
    echo "Installing fix pack."
    set -e
    ./installFixPack -f nobackup -n -b #{node[:db2][:installDir]}
    touch #{tmp}/fixpack_inst_marker
  EOH
end

# Start the DB2 server
bash "start_db2" do
  user "root"
  cwd "/migration"
  code <<-EOH
  ./startDb2.sh
  EOH
end

# Create backup directory 
directory "#{node[:db2][:backupDir]}" do
  user "db2inst1"
  group "db2iadm1"
  action :create
end

# Add firewall rule
bash "addfirewallrule for accessing the database" do
  user "root"
  code <<-EOH
    /sbin/iptables -A INPUT -p tcp --dport 50000 -j ACCEPT
    service iptables save
  EOH
  not_if "/sbin/iptables -L | grep dpt:db2c_db2inst1"
end

ruby_block "update_zookeeper" do
  block do
    dbstatusurls = []
    if node[:roles].include?("wcs_runtime_db")
      dbstatusurls << node[:db2][:wcs_runtime_db_url]
    end
    if node[:roles].include?("wcs_authoring_db")
      dbstatusurls << node[:db2][:wcs_authoring_db_url]
    end  

    dbstatusurls.each() do |dbstatusurl|
      require 'zk'
      require 'json'
      require 'rexml/document'

      z = ZK.new(zkep)
      db2Info = {:ready => true}
      if z.exists?(dbstatusurl)
        z.set(dbstatusurl, db2Info.to_json)
      else 
        z.create(dbstatusurl, db2Info.to_json, {:or => :set})
      end
      # Track the publish
      info = JSON.parse(z.get(node[:client][:status]).first)
      info.last[:publish] = {:url => dbstatusurl, :time => Time.now.to_i}
      z.create(node[:client][:status], info.to_json, {:or => :set})
    end
  end
end

template "#{tmp}/clean-installers.sh" do
  source "clean-installers.sh.erb"
  backup false
  owner "root"
  group "root"
  mode 0755
  variables(
    :tmpdir=>inst_temp
  )
  action :create
end

stopinstr :db2
